package preloader
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import mx.events.FlexEvent;
    import mx.preloaders.DownloadProgressBar;
 
    public class CustomPreloader extends DownloadProgressBar
    {
       	[Embed(source="loader.swf")]
        public var WelcomeScreenGraphic:Class;
        
 		public var wcs:MovieClip;
        public var _preloader:Sprite;
        
 		public function CustomPreloader():void
        {
            super();
            wcs = new WelcomeScreenGraphic();
            addChild(wcs);
            wcs.gotoAndStop(0);
        }
 
        public override function set preloader(preloaderss:Sprite):void
        {
        	_preloader = preloaderss
            preloaderss.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);
            preloaderss.addEventListener(Event.COMPLETE, onSWFDownloadComplete);
            preloaderss.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
            preloaderss.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
 			_preloader.visible = false;
        }
        
        private function centerPreloader():void
        {
	  		if ((stage.stageWidth > 0) && (_preloader)) 
			{  
				x = (stage.stageWidth - 112) / 2;  
				y = (stage.stageHeight - 115) / 2;  
				_preloader.visible = true;
			}  
        }
 
        private function onSWFDownloadProgress(event:ProgressEvent):void
        {
            centerPreloader();
        }
 
        private function onSWFDownloadComplete(event:Event):void
        {
            wcs.gotoAndStop(100);
        }
 
        private function onFlexInitProgress(event:FlexEvent):void
        {
            wcs.gotoAndStop(100);
        }
 
        private function onFlexInitComplete(event:FlexEvent):void
        {
            wcs.gotoAndStop(100);
            dispatchEvent( new Event(Event.COMPLETE));
        }
    }
}
